package com.zhy.learning.demo.cal;

import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSONObject;
import com.zhy.learning.demo.util.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhy
 * @date 2020/3/17 2:06 下午
 * @description 利息计算类
 */
public class InterestCal {

    private static final Logger logger = LoggerFactory.getLogger(InterestCal.class);

    /**
     * 等额本息
     * （本金*月利率*（1+月利率）^还款月数）/ （1+月利率^还款月数-1）
     *
     * @param principal 本金（元）
     * @param yearRate  年利率(比如：6.37%)
     * @param year      还款年数
     * @return
     */
    public static List<RepaymentPlan> averageCapitalPlusInterest(double principal, double yearRate, int year) {
        // 月利率
        double monthRate = yearRate / 1200;
        // 还款月数
        int month = year * 12;
        // 月供 = （本金*月利率*（1+月利率）^还款月数）/ （1+月利率^还款月数-1）
        double monthPay = (principal * monthRate * Math.pow(1 + monthRate, month)) / (Math.pow(1 + monthRate, month) - 1);
        logger.info("等额本息：{}", monthPay);
        List<RepaymentPlan> list = new ArrayList<>(month);
        RepaymentPlan repaymentPlan = null;
        double remainPrincipal = principal;
        double monthInterest = 0.00;
        double monthPrincipal = 0.00;
        for (int i = 0; i < month; i++) {
            repaymentPlan = new RepaymentPlan();
            if (i == month - 1) {
                monthPrincipal = remainPrincipal;
                monthInterest = monthPay - monthPrincipal;
            } else {
                // 本月还款利息
                monthInterest = remainPrincipal * monthRate;
                // 本月还款本金
                monthPrincipal = monthPay - monthInterest;
            }
            remainPrincipal = remainPrincipal - monthPrincipal;
            repaymentPlan.setRemainPrincipal(remainPrincipal);
            repaymentPlan.setMonthInterest(monthInterest);
            repaymentPlan.setMonthPay(monthPay);
            repaymentPlan.setMonthPrincipal(monthPrincipal);
            list.add(repaymentPlan);
        }
        return list;
    }

    /**
     * 等额本金
     * 公式：每月偿还本金=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率
     *
     * @param principal 本金(元)
     * @param yearRate  年利率(比如：6.37%)
     * @param year      还款年数
     * @return
     */
    public static List<RepaymentPlan> averageCapital(double principal, double yearRate, int year) {
        // 月利率
        double monthRate = yearRate / 1200;
        // 还款月数
        int month = year * 12;
        // 每月还款本金
        double monthPrincipal = principal / month;
        List<RepaymentPlan> list = new ArrayList<>(month);
        RepaymentPlan repaymentPlan = null;
        double monthInterest = 0.00;
        double remainPrincipal = principal;
        double monthPay = 0.00;
        for (int i = 0; i < month; i++) {
            if (i == 359) {
                System.out.println(1);
            }
            repaymentPlan = new RepaymentPlan();
            // 月供
            monthPay = monthPrincipal + remainPrincipal * monthRate;
            // 每月还款利息
            monthInterest = monthPay - monthPrincipal;
            // 剩余本金
            remainPrincipal = remainPrincipal - monthPrincipal;
            repaymentPlan.setRemainPrincipal(remainPrincipal);
            repaymentPlan.setMonthInterest(monthInterest);
            repaymentPlan.setMonthPay(monthPay);
            repaymentPlan.setMonthPrincipal(monthPrincipal);
            list.add(repaymentPlan);
        }
        return list;
    }

    public static void main(String[] args) {
        List<RepaymentPlan> repaymentPlans = InterestCal.averageCapitalPlusInterest(1000000, 6.37, 30);
        List<RepaymentPlan> repaymentPlans1 = InterestCal.averageCapital(1000000, 6.37, 30);
        ArrayList<ExcelUtil.MultipleSheelPropety> dataList = new ArrayList<>();

        Sheet sheet = new Sheet(1, 0);
        sheet.setSheetName("sheet1");
        ExcelUtil.MultipleSheelPropety multipleSheelPropety = new ExcelUtil.MultipleSheelPropety();
        multipleSheelPropety.setData(repaymentPlans);
        multipleSheelPropety.setSheet(sheet);

        dataList.add(multipleSheelPropety);

        Sheet sheet2 = new Sheet(2, 0);
        sheet2.setSheetName("sheet2");
        ExcelUtil.MultipleSheelPropety multipleSheelPropety2 = new ExcelUtil.MultipleSheelPropety();
        multipleSheelPropety2.setData(repaymentPlans1);
        multipleSheelPropety2.setSheet(sheet2);

        dataList.add(multipleSheelPropety2);

        ExcelUtil.writeWithMultipleSheel("/Users/qudian/zhy/zhyTest.xlsx", dataList);


    }
}
